<?php
/*
	本插件：$this->model('tixian')->方法名();
	其他地方调用：model('jy_kuaijiefee/tixian')->方法名();
*/
namespace jy_kuaijiefee;
defined('BY_JYA') or exit('error');
class tixian extends \plugin{
	public function addall($year='',$month=''){
		$fields=array('id','setting','mid','star','groupid','isrecommand','salecate','cateid','mobile','realname','status','jointime','accounttime','logo','merchname','upass','uname','dd_shop_id');
		$list=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'pluginname'=>'jy_kuaijiefee'),$fields);
		$setting=plugin('jy_merch')->plugin_setting('setting');
		foreach($list as $merch){
			$this->step1($merch,$year,$month,$setting);
		}
		return $items;
	}
	public function step1($merch=array(),$year='',$month='',$setting=array()){
		$merchid=intval($merch['id']);
		$year=$year;
		$month=$month>9?$month:'0'.$month;
		$params=array('status'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'clearstatus'=>0);
		if(!$year && $setting['balance_day']){
		//结算天数
			$time=TIMESTAMP-intval($setting['balance_day'])*86400;
			$this->session('balance_time',$time);
			$params['finishtime <']=$time;
		}elseif($year){
			$starttime = strtotime($year.$month."01000000");
			$firstdaystr = date("Y-m-01", $starttime);
			$endtime = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
			$params['createtime between']=array($starttime,$endtime);
		}
		//print_r($endtime.'~~'.$starttime);exit;
		$pindex = max(1, 1);
		$psize = 1000;
		$total='';
		$fields=array('id','mid','price','paytype','api_data','pay_id','createtime','peisong_id','dispatchprice','clearstatus','status','merchid','buytype','out_upfee','isdish');
		$ordergoods=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields);
		if(!count($ordergoods)){
			return '';
		}
		if($ordergoods){
				$salsecount=count($ordergoods);
				$item['ordermoney']=0;
				$item['commission_money']=0;
				$item['allgoodsfee']=0;
				$sys_choufee=0;
				$item['peifee']=0;
				$item['out_upfee']=0;
				$item['orderids']=array();
				if($this->plugin_exists('jy_commission')){
					$jy_commission=1;
				}
				$item['pingfee']=0;
				$item['dafee']=0;
				$item['fuwufee']=0;
				$item['jieprice']=0;
				foreach($ordergoods as $k=>$v){
					//剔除商户独立支付的
					$payonemerchid=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['pay_id']),'onemerchid');
					if(($v['paytype']=='wechat' || $v['paytype']=='credit') && $payonemerchid){
						pdo_update('jy_weishop_order',array('clearstatus'=>5),array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['id']));
					}
					$isclear=pdo_get('jy_weishop_order_goods',array('clearstatus >='=>1,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$v['id']));
					if($isclear['id']){
						pdo_update('jy_weishop_order',array('clearstatus'=>$isclear['clearstatus']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['id']));
						continue;
					}
					$status=$v['status'];
					$get_choufee=model('jy_merch/commission')->get_choufee($v,true,$merchid,1);
					$sys_choufee +=$get_choufee['commission_money'];
					if($jy_commission){
						$commission_money=model('jy_commission/commission')->get_orderfee($v['id']);
					}
					$item['ordermoney'] +=$v['price'];
					$item['out_upfee'] +=$v['out_upfee'];
					$item['commission_money'] +=$commission_money;
					$item['allgoodsfee'] +=$get_choufee['allgoodsfee'];
					$item['orderids'][]=$v['id'];
					if($v['peisong_id']){
						$sys_peiprice=pdo_getcolumn('jy_merch_order',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$v['id']),'sys_peiprice');
						$sys_peiprice=$sys_peiprice>0?$sys_peiprice:$v['dispatchprice'];
						$item['peifee'] +=$sys_peiprice;
					}
					$api_data=json_decode($v['api_data'],true);
					$item['pingfee'] +=$api_data['pingfee'];
					$item['dafee'] +=$api_data['dafee'];
					$v['jieprice']=$v['price']-$api_data['api_data']['pingfee']-$api_data['api_data']['dafee'];
					$item['jieprice'] +=$v['jieprice'];
				}
		}
		$item['year2']=$year;
		$item['month2']=$month;
		$item['koufee']=$item['allgoodsfee']+$item['peifee']+$item['out_upfee']-$item['ordermoney'];
		$params=array('status'=>1,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'clearstatus'=>0);
		if($time){
			$params['createtime <']=$time;
		}
		if($year){
			$params['createtime between']=array($starttime,$endtime);
			$item['endtime']=$endtime;
			$item['starttime']=$starttime;
		}else{
			$item['endtime']=$time?:TIMESTAMP;
		}
		$item['inpaymoney']=pdo_getsum('jy_merch_paylog',$params,'money');
		$item['inpaycount']=pdo_count('jy_merch_paylog',$params,'id');
		$allnum=$salsecount+$item['inpaycount'];
		if(!$allnum){
			return '';
		}
		$item['salsecount']=$salsecount;
		$item['ordermoney']=intval($item['ordermoney']*100)/100;
		$item['realprice']=$item['ordermoney']+$item['inpaymoney'];
		$item['price']=$item['realprice']-$item['peifee'];
		if($setting['educt_commission']==1){
			$item['price']=$item['price']-$item['commission_money'];
			$item['educt_commission']=1;
		}
		$msetting=json_decode($merch['setting'],true);
		$payrate=$merch['payrate'];
		$item['bankcard']=$msetting['bankcard'];
		$item['realname']=$msetting['realname'];
		$item['cname']=$msetting['cname'];
		//获取计算的抽成
		$item['sys_choufee']=$sys_choufee;
		$item['realpricerate']=$item['price']-$item['sys_choufee'];
		//扣手续费
		$item['feerate']=intval($setting['feerate']*100)/100;
		$item['ratefee']=$item['realpricerate']*$item['feerate']/100;
		$item['finalprice']=$item['jieprice']-$item['sys_choufee'];
		$item['ratefee']=price($item['ratefee']);
		$item['price']=price($item['price']);
		$item['commission_money']=price($item['commission_money']);
		$item['sys_choufee']=price($item['sys_choufee']);
		$item['finalprice']=intval($item['finalprice']*100)/100;
		$item['allgoodsfee']=price($item['allgoodsfee']);
		$item['koufee']=price($item['koufee']);
		$item['pay_type']='union';
		$item['tixian_timetype']=$setting['tixian_timetype'];
		if($item['finalprice']<=0){
			return '';
		}
		$kuaiset=array(
			'ordermoney'=>$item['ordermoney'],
			'pingfee'=>$item['pingfee'],
			'dafee'=>$item['dafee'],
			'fuwufee'=>$item['fuwufee'],
			'jieprice'=>$item['jieprice'],
			'sys_choufee'=>$item['sys_choufee'],
		);
		$data=array(
			'clearno'=>get_ordersn('jy_merch_clearing','clearno'),
			'starttime'=>$item['starttime'],
			'endtime'=>$item['endtime'],
			'salsecount'=>$item['salsecount'],
			'ordermoney'=>$item['ordermoney'],
			'inpaycount'=>$item['inpaycount'],
			'inpaymoney'=>$item['inpaymoney'],
			'realprice'=>$item['realprice'],
			'price'=>$item['price'],
			'realpricerate'=>$item['realpricerate'],
			'finalprice'=>$item['finalprice'],
			'jifen'=>$item['jifen'],
			'pay_type'=>$item['pay_type'],
			'pay_trade_no'=>$item['pay_trade_no'],
			'realname'=>$item['realname'],
			'alipay'=>$item['alipay'],
			'bankname'=>$item['bankname'],
			'bankcard'=>$item['bankcard'],
			'content'=>$item['content'],
			'orderids'=>ijson_encode($item['orderids']),
			'kuaiset'=>ijson_encode($kuaiset),
			'status'=>0,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$merchid;
		$data['createtime']=time();
		pdo_insert('jy_merch_clearing',$data);
		$clearid=pdo_insertid();
		$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'status'=>3,'clearstatus'=>0);
		if($item['orderids']){
			if($item['starttime'] || $item['endtime']){
				$params['createtime between']=array($item['starttime'],$item['endtime']);
			}
			$params['orderid']=$item['orderids'];
			pdo_update('jy_weishop_order_goods',array('clearstatus'=>1),$params);
			$params['id']=$item['orderids'];
			unset($params['orderid']);
			pdo_update('jy_weishop_order',array('clearstatus'=>1),$params);
		}
		$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'status'=>1);
		if($this->session('balance_time')){
			if($item['starttime']){
				$params['createtime between']=array($item['starttime'],$item['endtime']);
			}else{
				$params['createtime <']=$this->session('balance_time');
			}
		}
		if($item['orderids']){
			$params['order_id']=$item['orderids'];
		}
		pdo_update('jy_merch_order',array('status'=>2,'clearid'=>$clearid),$params);
		$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'status'=>1,'clearstatus'=>0);
		if($this->session('balance_time')){
			if($item['starttime']){
				$params['createtime between']=array($item['starttime'],$item['endtime']);
			}else{
				$params['createtime <']=$this->session('balance_time');
			}
		}
		pdo_update('jy_merch_paylog',array('clearstatus'=>1,'clearid'=>$clearid),$params);
		return '';
	}
}
?>